#!/usr/bin/python
# -*- coding: utf-8 -*-
#encoding=utf-8
#author=daimin
'''
Created on 2013年12月16日

@author: Administrator
'''

import db
import comm

class SdkUserInfo(db.DbUtil):
    """游戏用户信息
    """
    def __init__(self, **kwargs):
        db.DbUtil.__init__(self, **kwargs)
        ## 定义当前表的前缀
        self._tabname = "userinfo"


    def add_new_user(self, **reguser):
        regdate = comm.timestamp2datetime(reguser['regtime'])
        regdate = regdate.strftime("%Y%m%d")
        self._sadd(reguser['gameid'], regdate, comm.serialize(filter(lambda x:x[0]=='uname' or x[0]=='os' or x[0]=='channel' or x[0]=='svrid',\
                                                       reguser.items())))
        return self._hset(reguser['gameid'], reguser['uname'], reguser)
        
    def is_new_user(self, gameid, uname):
        return not self._hexist(gameid, uname)
                                        
                                        
    def update_user_info(self, **userinfo):
        olduserinfo = self.get_user(userinfo['gameid'], userinfo['uname'])
        if olduserinfo is None: return
        userinfoi = dict(filter(lambda x:x[0] != 'uname', userinfo.items()))
        fuserinfo = dict(map(lambda x:(x[0], userinfoi[x[0]]) if x[0] in userinfoi else x, olduserinfo.items()))
        return self.add_new_user(**fuserinfo)
    
    def get_users_date(self, sdate, gameid):
        sdate = sdate.strftime("%Y%m%d")
        return self._smember(gameid, sdate)
        
    def get_user(self, gameid, uname):
        return self._hget(gameid, uname)
        
        
        
               
class SdkPayInfo(db.DbUtil):
    """游戏支付信息
    """
    def __init__(self, **kwargs):
        db.DbUtil.__init__(self, **kwargs)
        self._tabname    = 'payinfo'
        

    def add_new_pay(self, **payinfo):
        return self._hset(payinfo['gameid'], payinfo['uname'], payinfo)
    
    def is_new_pay(self, uname, gameid):
        return not self._hexist(gameid, uname)
               
    def get_payuser(self, gameid, uname):
        return self._hget(gameid, uname)                         
                                        
    def update_pay_user(self, **payuser):
        oldpayuser = self.get_payuser(payuser['gameid'], payuser['uname'])
        if oldpayuser is None: return
        payuseri = dict(filter(lambda x:x[0] == 'paytime' or x[0] == 'paynum', payuser.items()))
        fpayuser = dict(map(lambda x:(x[0], payuseri[x[0]]) if x[0] in payuseri else x, oldpayuser.items()))
        self.add_new_pay(**fpayuser)
        
        
               